package ar.com.age.distribuidora.presentacion.converter;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import ar.com.age.distribuidora.dominio.Empleado;
import ar.com.age.distribuidora.presentacion.util.ServiceLocator;

@FacesConverter (value = "empleadoVentaConverter", forClass = Empleado.class)
public class EmpleadoVentaConverter implements Converter {

	   private Empleado empleado = null;
		
		public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
			
	        List<Empleado> lista = ServiceLocator.getUsuarioService().buscarEmpleado("", "", "", "", "Venta");
	        
		    if (arg2.equals("")) { 
				
	            return this.empleado;  
	        } else {  
	            try {  
	                int number = Integer.parseInt(arg2);  
	                for (Empleado l : lista) {  
	                    if (l.getId() == number) {  
	                		
	                		this.empleado = l;
	                        return this.empleado;  
	                    }  
	                }  
	  
	            } catch(NumberFormatException exception) {  
	                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));  
	            }  
	        }  

	       return this.empleado;
		}

		public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

			
			 if (arg2 == null || arg2.equals("")) {
		            return "";
		        } else {
		        	
		            return String.valueOf(((Empleado) arg2).getId());
		        }
		}

}
